package 剑指Offer1;

public class _28两个链表的第一个公共节点 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) return null;
        ListNode tA = headA,tB = headB;
        int count = 0;

        while(tA!=null){
            count++;
            tA = tA.next;
        }

        while(tB!=null){
            count--;
            tB = tB.next;
        }

        if(tA!=tB) return null;

        tA = headA;tB = headB;

        if(count>0){
            while(count!=0){
                count--;
                tA = tA.next;
            }
        }else{
            while(count!=0){
                count++;
                tB = tB.next;
            }
        }

        while(tA!=tB){
            tA = tA.next;
            tB = tB.next;
        }
        return tA;
    }
}
